% 带限内插法从采样重建信号
clc;
clear;

period = 2;
sliceS = 1000;% 原信号频率2*pi*1/sliceS
sliceP = 10;% 采样信号频率2*pi*1/sliceP
A = 10;% 信号幅值
% 图一 
figure(1);
% 第一部分 准备绘制原信号，最后叠上重建信号
subplot(2,1,1);

% 绘制原信号
tics = 0:1/sliceS:period;
sig = A*sin(2*pi*tics);
plot(tics,sig,'r');
hold on;
title('原信号和重建信号的波形');

% 第二部分，准备绘制采样序列
subplot(2,1,2);

% 获取采样值
ticp = 0:1/sliceP:period;
sample = A*sin(2*pi*ticp);%Ws=20Π
stem(ticp,sample);
title('采样序列');
% 插值
for i = 1:period*sliceP
    resig(:,i) = sample(i).*(sin(sliceP*pi*(tics-ticp(i)))./(sliceP*pi*(tics-ticp(i)))); %rebuild fre Wc=10Π
    figure(2);
    subplot(2,1,1);
    plot(tics,resig(:,i))
    hold on;
end

% 计算重建信号——叠加各插值结果
subplot(2,1,2);
reBuildSig = zeros(size(resig,1));
for i = 1:period*sliceP
    reBuildSig = reBuildSig +  resig(:,i); 
end

% 绘制重建信号过程
subplot(2,1,1)
plot(tics,reBuildSig,'g');
title('插值得到的各波和重建信号')

subplot(2,1,2)
plot(tics,reBuildSig,'p')
title('重建信号')

% 把重建信号绘制到原信号所在坐标图中
figure(1);
subplot(2,1,1)
plot(tics,reBuildSig,'b')
legend('原信号','重建信号');

%% last

% % rebuild with hand-limited interpolation
% clc;
% clear;
% 
% period = 1;
% sliceS = 1000;%W
% sliceP = 10;%Ws
% A = 10;% amplitude
% % get new board 
% figure(1);
% % chose the first part of the board
% subplot(2,1,1);
% 
% 
% tics = 0:1/sliceS:period;
% sig = A*sin(2*pi*tics);
% plot(tics,sig,'r');
% hold on;
% title('wave of signal source and rebuild signal');
% % chose the second part of the board
% subplot(2,1,2);
% 
% 
% % set samlpe tic and get sample result as sample[]
% ticp = 0:1/sliceP:period;
% sample = A*sin(2*pi*ticp);%Ws=20Π
% stem(ticp,sample);
% title('wave of the sample result');
% % interpolation
% for i = 1:11
%    resig(:,i) = sample(i).*(sin(sliceP*pi*(tics-ticp(i)))./(sliceP*pi*(tics-ticp(i)))); %rebuild fre Wc=10Π
%     figure(2);
%    subplot(2,1,1);
%    plot(tics,resig(:,i))
%    hold on;
% end
% % count rebuild result 
% subplot(2,1,2);
% reBuildSig = zeros(size(resig,1));
% for i = 1:11
%    reBuildSig = reBuildSig +  resig(:,i); 
% end
% plot(tics,reBuildSig)
% title('wave of rebuild signal')
% subplot(2,1,1)
% plot(tics,reBuildSig,'g');
% title('wave of interpolation and rebuild signal')
% 
% % draw rebuild line on the first board with signal source
% figure(1);
% subplot(2,1,1)
% plot(tics,reBuildSig,'b')
% legend('source','rebuild');




